package com.ktjiaoyu.server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjiaoyu.server.domain.pojo.Admin;
import com.ktjiaoyu.server.service.AdminService;
import com.ktjiaoyu.server.utils.PageUtil;
import com.ktjiaoyu.server.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auth wzy
 * @date 2023/2/14 16:07
 */
@Api(tags = "Admin相关接口")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 显示管理员分页列表信息
     * @param currPageNo 当前页码
     * @param pageSize 每页显示的数据行数
     * @param name 按用户名或手机号进行模糊查询
     */

    @ApiOperation(value = "获取用户列表数据接口")
    @GetMapping("/list")
    public ResponseResult list(@RequestParam(value = "currPageNo", defaultValue = "1") Integer currPageNo,
                       @RequestParam(value = "pageSize", defaultValue = "4")Integer pageSize,
                       @RequestParam(value = "name", required = false) String name,
                       @RequestParam(value = "sort")String sort){

        Page<Admin> page = new Page<Admin>(currPageNo, pageSize);
        IPage<Admin> adminIPage = adminService.selectByPage(page, name, sort);

        PageUtil<Admin> adminPageUtil = new PageUtil<Admin>(
                Math.toIntExact(adminIPage.getTotal()),
                Math.toIntExact(adminIPage.getCurrent()),
                adminIPage.getRecords());

        return ResponseResult.success(adminPageUtil);
    }

    @ApiOperation(value = "获取当前登录用户的信息")
    @GetMapping("/info")
    public ResponseResult userInfo(){
        Admin admin = adminService.getUserInfo();
        return ResponseResult.success(admin);
    }
}
